﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EntityComponentSystem.Framework.Systems;

namespace EntityComponentSystem.Framework
{
    public abstract class Behavior
    {
        public abstract string Name { get; }
        public abstract void AttachEntity(GameEngine ge, int entity);
        public abstract void DeattachEntity(GameEngine ge, int entity);
    }

    public abstract class Behavior<T> : Behavior
        where T : IBehaviorSystem
    {
        private string name = null;
        public Behavior(string systemName, string name)
        {
            this.SystemName = systemName;
            this.name = systemName + "." + name;
        }

        public override string Name { get { return name; } }
        public string SystemName { get; private set; }
        public T System { get; private set; }
        public override void AttachEntity(GameEngine ge, int entity)
        {
            if (System == null)
                System = ge.GetBehaviorSystem<T>(SystemName);

            if (System != null)
                System.AddBehavior(entity, this, GetEntityBehavior(System, ge, entity));
        }

        public override void DeattachEntity(GameEngine ge, int entity)
        {
            if (System != null)
                System.RemoveBehavior(entity, this);
        }

        protected abstract Action GetEntityBehavior(T system, GameEngine ge, int entity);
    }
}
